package ma.ensao.egicom.core.services.implementations;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ma.ensao.egicom.core.dao.interfaces.CategoryDAO;
import ma.ensao.egicom.core.dao.interfaces.ProductDAO;
import ma.ensao.egicom.core.dao.interfaces.SupplierDAO;
import ma.ensao.egicom.core.dto.ProductDto;
import ma.ensao.egicom.core.model.Category;
import ma.ensao.egicom.core.model.Product;
import ma.ensao.egicom.core.model.Supplier;
import ma.ensao.egicom.core.services.interfaces.ProductService;

import org.scub.foundation.framework.base.exception.BusinessException;
import org.scub.foundation.framework.base.exception.TechnicalException;
import org.scub.foundation.framework.base.mapping.util.MapperDozerBean;
import org.scub.foundation.framework.base.messagesource.MessageSourceUtil;

public class ProductServiceImpl implements ProductService {

    private CategoryDAO categoryDao;

    private ProductDAO productDao;

    private SupplierDAO supplierDao;

    private MapperDozerBean mapperDozerBean;

    private MessageSourceUtil messageSourceUtil;

    @Override
    public List<ProductDto> getProductsByCategory(Long idCategory) {
        // On vérifie les paramètres
        if (idCategory == null) {
            throw new TechnicalException("TechnicalException");
        }

        // On verifie qu'une category ayant l'identifiant passé en paramètre existe bien
        Category categoryBean = categoryDao.findById(idCategory);
        if (categoryBean == null) {
            throw new BusinessException("BusinessException");
        }
        return mapProductList(categoryBean.getProducts());
    }

    @Override
    public List<ProductDto> getNewProducts() {
        return mapProductList(new HashSet(productDao.findNewProducts()));
    }

    @Override
    public boolean addNewProduct(ProductDto product) {
        try {
            Product productBean = mapperDozerBean.map(product, Product.class);
            // Get Category
            Category categoryBean = categoryDao.findById((long) (product.getCategory().getId()));
            if (categoryBean == null) throw new BusinessException("BusinessException");
            productBean.setCategory(categoryBean);
            // Get Supplier
            Supplier supplierBean = supplierDao.findById((long) (product.getSupplier().getId()));
            if (supplierBean == null) throw new BusinessException("BusinessException");
            productBean.setSupplier(supplierBean);

            productDao.save(productBean);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    private List<ProductDto> mapProductList(Set<Product> products) {
        final List<ProductDto> productDtoList = new ArrayList<ProductDto>();
        for (Product product : products) {
            productDtoList.add((ProductDto) mapperDozerBean.map(product, ProductDto.class));
        }
        return productDtoList;
    }

    public CategoryDAO getCategoryDao() {
        return categoryDao;
    }

    public void setCategoryDao(CategoryDAO categoryDao) {
        this.categoryDao = categoryDao;
    }

    public MapperDozerBean getMapperDozerBean() {
        return mapperDozerBean;
    }

    public MessageSourceUtil getMessageSourceUtil() {
        return messageSourceUtil;
    }

    public void setMapperDozerBean(MapperDozerBean mapperDozerBean) {
        this.mapperDozerBean = mapperDozerBean;
    }

    public void setMessageSourceUtil(MessageSourceUtil messageSourceUtil) {
        this.messageSourceUtil = messageSourceUtil;
    }

    public ProductDAO getProductDao() {
        return productDao;
    }

    public void setProductDao(ProductDAO productDao) {
        this.productDao = productDao;
    }

    public SupplierDAO getSupplierDao() {
        return supplierDao;
    }

    public void setSupplierDao(SupplierDAO supplierDao) {
        this.supplierDao = supplierDao;
    }

}
